package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.WorkOrderConfig;
import com.tbit.main.service.TokenService;
import com.tbit.main.service.WorkOrderConfigService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 区域工单开关设置
 *
 * @author LMY
 * @create 2021-09-03 10:03
 */

@RestController
@RequestMapping("/workOrderConfig")
public class WorkOrderConfigController {

    @Autowired
    private WorkOrderConfigService workOrderConfigService;
    @Autowired
    private TokenService tokenService;

    /**
     * 根据区域id查询
     *
     * @param token
     * @param accountId
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(String token, Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        AccountUser accountUser = tokenService.getAccount(token);

        WorkOrderConfig workOrderConfig = workOrderConfigService.getByAccountId(accountId);
        if (workOrderConfig == null) {
            workOrderConfig = new WorkOrderConfig();
            workOrderConfig.setAccountId(accountId);
            workOrderConfig.setPowerChangeSwitch(false);
            workOrderConfig.setPowerChangeNumber(120);
            workOrderConfig.setMoveSwitch(false);
            workOrderConfig.setMoveNumber(120);
            workOrderConfig.setRepairSwitch(false);
            workOrderConfig.setRepairNumber(120);
            workOrderConfig.setRedeploySwitch(true);
        }

        return JsonResult.succ(workOrderConfig);
    }

    /**
     * 添加或修改
     *
     * @param token
     * @param workOrderConfig
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.WORKORDERCONFIG, methods = MethodEnum.WORKORDERCONFIG_EDIT)
    public JsonResult add(String token, WorkOrderConfig workOrderConfig) {

        Assert.notNull(workOrderConfig.getAccountId(), "AccountUser.agentId.notNull");
        AccountUser accountUser = tokenService.getAccount(token);

        LogUtil.info(accountUser.getAccountUserId() + "工单配置：" + workOrderConfig.toString());

        WorkOrderConfig workOrderConfigDB = workOrderConfigService.getByAccountId(workOrderConfig.getAccountId());
        if (workOrderConfigDB == null) {
            workOrderConfigService.insert(workOrderConfig);
        } else {
            ObjectUtil.assign(workOrderConfig, workOrderConfigDB, null);
            workOrderConfigService.update(workOrderConfigDB);
            LogUtil.info(accountUser.getAccountUserId() + "修改工单配置：" + workOrderConfig.toString());
        }

        return JsonResult.succ();
    }

}
